package com.huika.renrench.control.my.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.huika.lib.utils.StringUtils;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAda;
import com.huika.renrench.support.bean.MyShopListBean;

public class MyShopAdapter extends BaseAda<MyShopListBean> {
	private OnCheckItem onCheckItem;
	private ArrayList<CheckBox> checkBoxs;

	public MyShopAdapter(Context context, OnCheckItem onCheckItem) {
		super(context);
		this.onCheckItem = onCheckItem;
		checkBoxs = new ArrayList<CheckBox>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_tranfer_info, parent, false);
			vh = new ViewHolder(convertView);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		MyShopListBean bean = getItem(position);
		String imgUrl = bean.getDefaultImage();
		if (imgUrl != null && !TextUtils.isEmpty(imgUrl)) {
			GlobalApp.loadImgLoader(vh.ivIcon, bean.getDefaultImage());
		} else {
			vh.ivIcon.setImageResource(R.drawable.icon_default_small);
		}
		vh.cb.setTag(position);
		checkBoxs.add(vh.cb);
		// if (bean.isEidt()) {
		// vh.cb.setVisibility(View.VISIBLE);
		if (bean.isCheck()) {
			vh.cb.setChecked(true);
		} else {
			vh.cb.setChecked(false);
		}
		// }else{
		// vh.cb.setVisibility(View.GONE);
		// }

		vh.tvTitle.setText(bean.getShopName());
		if (!"0".equals(bean.getPrice())&&!"null".equals(bean.getPrice())&&!TextUtils.isEmpty(bean.getPrice()) && !"面议".equals(bean.getPrice())) {
			vh.tvPrice.setText(String.format("租金：￥%s", StringUtils.getWan(Double.parseDouble(bean.getPrice()))));
		} else {
			vh.tvPrice.setText(String.format("租金：￥%s", "面议"));
		}
		if (!"null".equals(bean.getAcreage())&&!TextUtils.isEmpty(bean.getAcreage())) {
			vh.tvArea.setText(String.format("面积：%s㎡", StringUtils.getWanArea(Double.parseDouble(bean.getAcreage()))));
		}
		vh.tvAddress.setText(bean.getAddress());
		vh.tvTime.setText(String.format("更新时间：%s", bean.getUpdateDT()));
		return convertView;
	}

	class ViewHolder {
		public ImageView ivIcon;
		public TextView tvTitle;
		public TextView tvPrice;
		public TextView tvArea;
		public TextView tvAddress;
		public TextView tvTime;
		public CheckBox cb;

		public ViewHolder(View convertView) {
			ivIcon = (ImageView) convertView.findViewById(R.id.iv_img_iti);
			tvTitle = (TextView) convertView.findViewById(R.id.tv_title_iti);
			tvPrice = (TextView) convertView.findViewById(R.id.tv_learn_price_iti);
			tvArea = (TextView) convertView.findViewById(R.id.tv_area_iti);
			tvAddress = (TextView) convertView.findViewById(R.id.tv_address_iti);
			tvTime = (TextView) convertView.findViewById(R.id.tv_time_iti);
			cb = (CheckBox) convertView.findViewById(R.id.cb_iti);
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					int position = (Integer) buttonView.getTag();
					onCheckItem.onCheckItem(position, isChecked);
				}
			});
		}

	}

	public interface OnCheckItem {
		public void onCheckItem(int position, boolean isChecked);
	}

	public void showCbs() {
		for (int i = 0; i < checkBoxs.size(); i++) {
			checkBoxs.get(i).setVisibility(View.VISIBLE);
		}
	}

	public void hideCbs() {
		for (int i = 0; i < checkBoxs.size(); i++) {
			checkBoxs.get(i).setVisibility(View.GONE);
		}
	}

}
